package com.itheima.service.impl;

import com.itheima.dao.SysUserPostDao;
import com.itheima.pojo.SysUserPost;
import com.itheima.service.SysUserPostService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * (SysUserPost)表服务实现类
 *
 * @author makejava
 * @since 2022-01-16 15:48:53
 */
@Service("sysUserPostService")
public class SysUserPostServiceImpl implements SysUserPostService {
    @Resource
    private SysUserPostDao sysUserPostDao;

    /**
     * 通过ID查询单条数据
     *
     * @param userId 主键
     * @return 实例对象
     */
    @Override
    public SysUserPost queryById(Long userId) {
        return this.sysUserPostDao.queryById(userId);
    }

    /**
     * 分页查询
     *
     * @param sysUserPost 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<SysUserPost> queryByPage(SysUserPost sysUserPost, PageRequest pageRequest) {
        long total = this.sysUserPostDao.count(sysUserPost);
        return new PageImpl<>(this.sysUserPostDao.queryAllByLimit(sysUserPost, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param sysUserPost 实例对象
     * @return 实例对象
     */
    @Override
    public SysUserPost insert(SysUserPost sysUserPost) {
        this.sysUserPostDao.insert(sysUserPost);
        return sysUserPost;
    }

    /**
     * 修改数据
     *
     * @param sysUserPost 实例对象
     * @return 实例对象
     */
    @Override
    public SysUserPost update(SysUserPost sysUserPost) {
        this.sysUserPostDao.update(sysUserPost);
        return this.queryById(sysUserPost.getUserId());
    }

    /**
     * 通过主键删除数据
     *
     * @param userId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long userId) {
        return this.sysUserPostDao.deleteById(userId) > 0;
    }
}
